import XEUtils from "xe-utils";
import { useEventListener } from "@vueuse/core";
import type { DirectiveBinding } from "vue";
/** 防抖（v-optimize或v-optimize:debounce）、节流（v-optimize:throttle）指令 */
export default {
    mounted(el: HTMLElement, binding: DirectiveBinding) {
        const { value } = binding;
        const optimizeType = binding.arg ?? "debounce";
        const type = ["debounce", "throttle"].find((t) => t === optimizeType);
        if (type) {
            if (value && value.event && XEUtils.isFunction(value.fn)) {
                let params = value?.params;
                if (params) {
                    if (XEUtils.isArray(params) || XEUtils.isObject(params)) {
                        params = XEUtils.isObject(params) ? Array.of(params) : params;
                    } else {
                        throw new Error("[Directive: optimize]: `params` must be an array or object");
                    }
                }
                const options = {
                    leading: value?.leading ?? false,
                    trailing: value?.trailing ?? false,
                };
                // Register using addEventListener on mounted, and removeEventListener automatically on unmounted
                useEventListener(el, value.event, type === "debounce" ? XEUtils.debounce(params ? () => value.fn(...params) : value.fn, value?.timeout ?? 200, options) : XEUtils.throttle(params ? () => value.fn(...params) : value.fn, value?.timeout ?? 1000, options));
            } else {
                throw new Error("[Directive: optimize]: `event` and `fn` are required, and `fn` must be a function");
            }
        } else {
            throw new Error("[Directive: optimize]: only `debounce` and `throttle` are supported");
        }
    },
};
